//
//  ZGSArtleViewController.m
//  MapleLeaf
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 AntennaBabyTeam. All rights reserved.
//

#import "ZGSArtleViewController.h"
#import "ZGSOneSentenceModel.h"
#import "ZGSMineViewController.h"
#import "ZGSOneArticleViewController.h"
#import "ZGSQuestionController.h"
@interface ZGSArtleViewController ()

@property (nonatomic, retain) UIView *headBgView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) MBProgressHUD *hud;
@property (nonatomic, retain) ZGSOneSentenceModel *sentence;


@end

@implementation ZGSArtleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self creatData];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)creatData {
    [self createHeadBgView];
    
    AFNetworkReachabilityManager *nManager = [AFNetworkReachabilityManager sharedManager];
    
    // 开始检测
    [nManager startMonitoring];
    [nManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == 1 || status == 2) {
            NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
            formatter.dateFormat = @"yyyy-MM-dd";
            NSString *strDate = [formatter stringFromDate:date];
            
            NSDictionary *postDic = @{@"strDate":strDate, @"strRow":@"1"};

            [BaseAFN POSTURLByString:@"http://rest.wufazhuce.com/OneForWeb/one/getHp_N" bodyByDic:postDic AFNData:^(id result) {
                NSDictionary *dic = result[@"hpEntity"];
                self.sentence = [[ZGSOneSentenceModel alloc] init];
                [self.sentence setValuesForKeysWithDictionary:dic];
                
                [self.hud removeFromSuperview];
                
                [self createViews];

                
                
            }];
        }
        else {
            [self.hud removeFromSuperview];
            
            MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
            [self.view addSubview:hud];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"数据请求失败,请检查网络后重试";
            [hud showAnimated:YES whileExecutingBlock:^{
                sleep(1);
            } completionBlock:^{
                [hud removeFromSuperview];
            }];
        }
        
    }];
    
    // 停止检测
    [nManager stopMonitoring];

}

- (void)createHeadBgView {
    self.headBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.headBgView.backgroundColor = [UIColor colorWithRed:0.965f green:0.463f blue:0.220f alpha:1.00f];
    [self.view addSubview:self.headBgView];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 27, 35, 25);
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    backButton.tintColor = [UIColor whiteColor];
    backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:backButton];
    
//    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    shareButton.frame = CGRectMake(WIDTH - 40, 25, 25, 25);
//    [shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-share.png"] forState:UIControlStateNormal];
//    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.headBgView addSubview:shareButton];
    
    UIButton *sentenceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    sentenceButton.frame = CGRectMake(55, 27, 50, 25);
    [sentenceButton setTitle:@"句子" forState:UIControlStateNormal];
    sentenceButton.backgroundColor = [UIColor whiteColor];
    sentenceButton.tintColor = [UIColor colorWithRed:0.965f green:0.463f blue:0.220f alpha:1.00f];
    sentenceButton.layer.cornerRadius = 13;
    sentenceButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [self.headBgView addSubview:sentenceButton];
    
    UIButton *acticleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    acticleButton.frame = CGRectMake(110, 27, 50, 25);
    [acticleButton setTitle:@"文章" forState:UIControlStateNormal];
    acticleButton.tintColor = [UIColor whiteColor];
    acticleButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [acticleButton addTarget:self action:@selector(changePageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:acticleButton];
    
    UIButton *questionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    questionButton.frame = CGRectMake(165, 27, 50, 25);
    [questionButton setTitle:@"问题" forState:UIControlStateNormal];
    questionButton.tintColor = [UIColor whiteColor];
    questionButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    [questionButton addTarget:self action:@selector(changePageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headBgView addSubview:questionButton];
    
    
    UIButton *questionbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    questionbutton.frame = CGRectMake(110, 27, 50, 25);
    [questionbutton setTitle:@"a" forState:UIControlStateNormal];
    

}

- (void)createViews {
    
    UILabel *volLabel = [[UILabel alloc] initWithFrame:CGRectMake(18 / 667.0 * HEIGHT, 64 + 17 / 375.0 *WIDTH, 120, 20 / 667.0 * HEIGHT)];
    volLabel.font = [UIFont fontWithName:@"ArialUnicodeMS" size:15];
    volLabel.text = self.sentence.strHpTitle;
    [self.headBgView addSubview:volLabel];
    
    NSRange range = [self.sentence.strAuthor rangeOfString:@"&"];
    NSString *authorStr = [self.sentence.strAuthor substringToIndex:range.location];
    NSString *titleStr = [self.sentence.strAuthor substringFromIndex:range.location + 1];
    
    UILabel *imageAuthorLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 200 * 375.0 / WIDTH, 64 + 335 / 667.0 * HEIGHT, 182 * 375.0 / WIDTH, 15 / 667.0 * HEIGHT)];
    imageAuthorLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    imageAuthorLabel.textColor = [UIColor colorWithRed:0.573f green:0.573f blue:0.573f alpha:1.00f];
    imageAuthorLabel.textAlignment = NSTextAlignmentRight;
    imageAuthorLabel.text = authorStr;
    [self.headBgView addSubview:imageAuthorLabel];
    
    UILabel *imageTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 200 * 375.0 / WIDTH, 64 + 355 / 667.0 * HEIGHT, 182 * 375.0 / WIDTH, 15 / 667.0 * HEIGHT)];
    imageTitleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    imageTitleLabel.textColor = [UIColor colorWithRed:0.573f green:0.573f blue:0.573f alpha:1.00f];
    imageTitleLabel.textAlignment = NSTextAlignmentRight;
    imageTitleLabel.text = titleStr;
    [self.headBgView addSubview:imageTitleLabel];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(18 / 375.0 * WIDTH, 64 + 55 / 667.0 * HEIGHT, WIDTH - 36 / 375.0 * WIDTH, 260 / 667.0 * HEIGHT)];
    NSLog(@"%@", self.sentence.strThumbnailUrl);
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.sentence.strThumbnailUrl] placeholderImage:[UIImage imageNamed:@"backWhite.png"]];
    [self.view addSubview:self.imageView];
    
    NSString *dayStr = [self.sentence.strMarketTime substringFromIndex:self.sentence.strMarketTime.length - 2];
    
    NSString *monthStr = [self.sentence.strMarketTime substringWithRange:NSMakeRange(self.sentence.strMarketTime.length - 5, 2)];
    
    NSString *yearStr = [self.sentence.strMarketTime substringToIndex:4];
    
    UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(18 * WIDTH / 375.0, 64 + 390 * HEIGHT / 667.0, 60 * WIDTH / 375.0, 60 * HEIGHT / 667.0)];
    dayLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:39];
    dayLabel.textColor = [UIColor colorWithRed:0.965f green:0.463f blue:0.220f alpha:0.50f];
    dayLabel.text = dayStr;
    [self.headBgView addSubview:dayLabel];
    
    UILabel *monthLabel = [[UILabel alloc] initWithFrame:CGRectMake(18 * WIDTH / 375.0, 64 + 423 * HEIGHT / 667.0, 70 * WIDTH / 375.0, 50 * HEIGHT / 667.0)];
    monthLabel.font = [UIFont fontWithName:@"ArialUnicodeMS" size:12];
    monthLabel.textColor = [UIColor colorWithRed:0.659f green:0.659f blue:0.659f alpha:1.00f];
    NSInteger month = [monthStr integerValue];
    switch (month) {
        case 1:
            monthLabel.text = [NSString stringWithFormat:@"Jan,%@", yearStr];
            break;
        case 2:
            monthLabel.text = [NSString stringWithFormat:@"Feb,%@", yearStr];
            break;
        case 3:
            monthLabel.text = [NSString stringWithFormat:@"Mar,%@", yearStr];
            break;
        case 4:
            monthLabel.text = [NSString stringWithFormat:@"Apr,%@", yearStr];
            break;
        case 5:
            monthLabel.text = [NSString stringWithFormat:@"May,%@", yearStr];
            break;
        case 6:
            monthLabel.text = [NSString stringWithFormat:@"Jun,%@", yearStr];
            break;
        case 7:
            monthLabel.text = [NSString stringWithFormat:@"Jul%@", yearStr];
            break;
        case 8:
            monthLabel.text = [NSString stringWithFormat:@"Aug,%@", yearStr];
            break;
        case 9:
            monthLabel.text = [NSString stringWithFormat:@"Sep,%@", yearStr];
            break;
        case 10:
            monthLabel.text = [NSString stringWithFormat:@"Oct,%@", yearStr];
            break;
        case 11:
            monthLabel.text = [NSString stringWithFormat:@"Nov,%@", yearStr];
            break;
        case 12:
            monthLabel.text = [NSString stringWithFormat:@"Dec,%@", yearStr];
            break;
            
        default:
            break;
    }
    
    [self.headBgView addSubview:monthLabel];
    
    UIView *contentBgView = [[UIView alloc] init];
    contentBgView.backgroundColor = [UIColor colorWithRed:0.965f green:0.463f blue:0.220f alpha:0.90f];
    contentBgView.layer.cornerRadius = 5;
    [self.headBgView addSubview:contentBgView];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 * WIDTH / 375.0, 5 * WIDTH / 375.0, WIDTH - 118 * WIDTH / 375.0, 5000)];
    contentLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    contentLabel.numberOfLines = 0;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self.sentence.strContent];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:6];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.sentence.strContent.length)];
    
    contentLabel.attributedText = attributedString;
    [contentLabel sizeToFit];
    contentLabel.textColor = [UIColor whiteColor];
    [contentBgView addSubview:contentLabel];
    
    contentBgView.frame = CGRectMake(85 * WIDTH / 375.0, 64 + 395 * HEIGHT / 667.0, WIDTH - 103 * WIDTH / 375.0, contentLabel.frame.size.height + 15 * HEIGHT / 667.0);
    
    
    UIView *likeBgView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH - 50 * WIDTH / 375.0 - 20 * HEIGHT / 667.0, contentLabel.frame.size.height + 64 + 430 * HEIGHT / 667.0, 50 * WIDTH / 375.0 + 40 * HEIGHT / 667.0, 20 * HEIGHT / 667.0)];
    likeBgView.layer.borderWidth = 1;
    likeBgView.layer.borderColor = [[UIColor colorWithRed:0.804f green:0.804f blue:0.804f alpha:1.00f] CGColor];
    likeBgView.layer.cornerRadius = 10 * HEIGHT / 667.0;
    [self.view addSubview:likeBgView];
    
    UIImageView *likeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9 / 667.0 * HEIGHT, 3.5 / 667.0 * HEIGHT, 13 / 667.0 * HEIGHT, 13 / 667.0 * HEIGHT)];
    likeImageView.image = [UIImage imageNamed:@"iconfont-xihuanb.png"];
    [likeBgView addSubview:likeImageView];
    
    UILabel *likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(24 / 667.0 * HEIGHT, 3.5 / 667.0 * HEIGHT, 50 / 375.0 * WIDTH, 13 / 667.0 * HEIGHT)];
    likeLabel.font = [UIFont systemFontOfSize:13];
    likeLabel.textColor = [UIColor colorWithRed:0.382f green:0.382f blue:0.382f alpha:1.00f];
    likeLabel.text = self.sentence.strPn;
    [likeBgView addSubview:likeLabel];
    

    
}

#pragma mark - ButtonAction

- (void)backAction:(UIButton *)button {
    
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"cube";
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    ZGSMineViewController *userVC = [[ZGSMineViewController alloc] init];
    [self.navigationController pushViewController:userVC animated:YES];
}

- (void)changePageAction:(UIButton *)button {
    if ([button.titleLabel.text isEqualToString:@"文章"]) {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.0f;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = @"pageCurl";
        transition.subtype = kCATransitionFromRight;
        transition.delegate = self;
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        
        ZGSOneArticleViewController *oaVC = [[ZGSOneArticleViewController alloc] init];
        [self.navigationController pushViewController:oaVC animated:YES];
    }
    else if ([button.titleLabel.text isEqualToString:@"问题"]) {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.0f;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = @"pageCurl";
        transition.subtype = kCATransitionFromRight;
        transition.delegate = self;
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        
        ZGSQuestionController *oqVC = [[ZGSQuestionController alloc] init];
        [self.navigationController pushViewController:oqVC animated:YES];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
